-- 适用屏幕参数
SCREEN_RESOLUTION="640x1136";
SCREEN_COLOR_BITS=32;

UI = {
        { 'TextView{-请如实填写哦-}'                   },
        { 'InputBox{qweasdzxc}',           'inputPassword',     '密码:' },
	    { 'InputBox{0}',           'inputIndex',     '开始的账号序列号:' },

};

HomeColors = {    
    {"150", "200","0x5E","0x93","0x05", "0x5E9305"},  
    {"150", "850","0x6E","0xAF","0x0E", "0x6EAF0E"},  
}

LoginColors = {    
    {"50", "200", "0xF7","0xF7","0xF9","0xF7F7F9"},  
    {"320", "800","0x00","0x73","0xFF", "0x0073FF"},  
    {"50", "900", "0xF7","0xF7","0xF9","0xF7F7F9"},  
}

RegisterColors = {    
    {"611", "22", "0x00","0x7A","0xFF","0x007AFF"},  
    {"603", "28", "0x00","0x7A","0xFF","0x007AFF"},  
    {"618", "28", "0x00","0x7A","0xFF","0x007AFF"},  
}

GameInGameColors = {
    {"623", "983", "0x6A","0x1E","0x18","0x6A1E18"},  
    {"625", "1074", "0x66","0x1A","0x15","0x661A15"},  
    {"612", "980", "0x5F","0x08","0x00","0x5F0800"},  
    {"612", "1076", "0x60","0x00","0x00","0x600000"},  
    {"618", "1116", "0xFE","0xF7","0xB6","0xFEF7B6"},  
}	

--ohtercolors暂时没用
OtherColors = {    
    {"301", "752","0xD6","0xDE","0xD5", "0xD6DED5"},  
    {"353", "643","0xD6","0xHF","0xE4", "0xD6HFE4"},  
    {"543", "654","0xD6","0xDG","0xD4","0xD6DGD4"},  
}

function singleClick(i, x, y)
	touchDown(i, x, y)
	mSleep(200)
	touchUp(i)
end

function longTouch(fingerId,touchTime,x,y)
	touchDown(fingerId, x, y)
	mSleep(touchTime)
	touchUp(fingerId)
end	

function mzLog(s)
   	logDebug("[----"..s.."----]")	
end 

function mzSnapshotScreen(name)
	local tmpTime = getNetTime();
	if tmpTime ~= -1 then
		newTime = os.date("*t", tmpTime);
		local tmpTimeStr = string.format('%d-%02d-%02d %02d:%02d:%02d',newTime.year,newTime.month,newTime.day,newTime.hour,newTime.min,newTime.sec)  
		snapshotScreen("var/touchelf/mz/"..tmpTimeStr..name); 	
	else
		snapshotScreen("var/touchelf/mz/"..name); 	
	end	
end
---
function unknowLogic()
	mzLog("unknowLogic")
	mzSnapshotScreen("unknowLogic.jpg")
	local qmqjPackageName = "com.tianmashikong.qmqj.apps";
		
	if appRunning(qmqjPackageName) then
	  -- appKill(qmqjPackageName)
		local qmqjPackageName = "com.tianmashikong.qmqj.apps";  
		notifyMessage('未知状态')
	else
		appRun(qmqjPackageName)	
	end
	
	mSleep(3000)  	
	
end 

function reStartGame()
    mzLog("reStartGame")
	local qmqjPackageName = "com.tianmashikong.qmqj.apps";
	if appRunning(qmqjPackageName) then
	    appKill(qmqjPackageName)
		mSleep(3000)
		appRun(qmqjPackageName)	
	else
		appRun(qmqjPackageName)	
	end
		
	mSleep(4000)  	
end 

function homeLogic()
	mSleep(1730);
-- 切换账号
	singleClick(1,124,280)	
end 

function homeLoginLogic()
	--进入游戏
    mSleep(4500);
    singleClick(1, 132, 880)

    --选择角色进入游戏
    mSleep(4500);
    singleClick(1, 66, 626)
	
end	

function loginLogic(loginAccount,loginPassword)
	mSleep(2000)
	--删除一个缓冲账号
    singleClick(1, 360, 688)
    mSleep(1000);
	singleClick(1, 284, 698)
    mSleep(1000);
	mzLog("删除一个缓冲账号")	
	--输入账号
    singleClick(1, 358, 304)
	mSleep(1500);
	longTouch(2,2000,96,2000)--删除默认账户
	mSleep(1500);
	inputText(loginAccount)
	mzLog("输入账号"..loginAccount)	

	--输入密码
    mSleep(1000);
    singleClick(6, 470, 342)
    mSleep(1000);	
	longTouch(2,2000,96,2000)--删除默认账户
	mSleep(2000);	
  	inputText(loginPassword)
	mSleep(1500);	
	mzLog("输入密码"..loginPassword)	

	--点击输入完成	
    singleClick(1, 552, 952)
	
	--点击登录
	mSleep(1000)
	singleClick(1, 344, 896)	 	
		
	--判断是否是首页
	mSleep(5000)	
	local currentState = getCurrentState()		
	if currentState == 1 then 
		homeLoginLogic()
	end
		
end	

function lingQuMingXiang()

	mSleep(4128);
    touchDown(2, 168, 730)
    mSleep(102);
    touchUp(2)

    mSleep(3566);
    touchDown(6, 270, 766)
    mSleep(101);
    touchUp(6)

    mSleep(5179);
    touchDown(3, 554, 876)
    mSleep(100);
    touchUp(3)

    mSleep(1000);
end 

function gameInGameDailyJobLogic()
	--关闭福利对话框
    mSleep(2480);
    singleClick(1, 612, 1096)
	
	--点击每日必做	
    mSleep(3210);
    singleClick(1, 284, 1094)

	--点击资源找回
    mSleep(3000);
    singleClick(1, 532, 272)
    
   --一键找回
    mSleep(3000);
    singleClick(1, 58, 846)
	--确定
    mSleep(2000);
    singleClick(1, 180, 484)
	--关闭
    mSleep(3000);
    singleClick(9, 614, 1092)

    mSleep(1000);

	lingQuMingXiang()
--[[	
	local sum = 0;
	for i=0, 6 do
		--点击第一个金币找回
		mSleep(4000);
		singleClick(8, 238, 150)
		--点击对话框确定按钮	
		mSleep(2000);
		singleClick(7, 170, 464)
	end
	]]	
end 

function gameInGameEnterMapLogic()

end 

function registerLogic()
	mSleep(1176);
	singleClick(1,634,1098)
end


function getCurrentState()
	
	if(compare_color_Multi_point(HomeColors) == 0) then
		mzLog("[----Home State----]")	
	return 1		
	end	
	if(compare_color_Multi_point(LoginColors) == 0) then
		mzLog("[----Login State----]")	
		return 2		
	end		
	if(compare_color_Multi_point(RegisterColors) == 0) then
		mzLog("[----Register State----]")	
		return 3		
	end	
	if(compare_color_Multi_point(GameInGameColors) == 0) then 
		mzLog("[----GameInGame State----]")	
		return 4
	end		
		
	if(compare_color_Multi_point(OtherColors) == 0) then
		mzLog("[----Ohters State----]")	
		return 0		
	end			
	return 0		
end

function compare_color_Multi_point(xyColors)
	for i, v in pairs(xyColors) do  
		if type(v) == "table" then  						
			x = tonumber(v[1])
			y = tonumber(v[2])			
			myR = tonumber(v[3])
			myG = tonumber(v[4])
			myB = tonumber(v[5])
			color = tonumber(v[6])
						
			--logDebug(color)			
			--logDebug("参数内容是:")			
			--logDebug(v[1])	
			--logDebug(v[2])
			--logDebug(v[3])
			--logDebug(v[4])
			--logDebug(v[5])
		
			r, g, b = getColorRGB(x,y)	
			--logDebug("获取的RGB是:")	
			--logDebug(r)
			--logDebug(g)
			--logDebug(b)	
						
			ttR = r - myR
			--logDebug(ttR)	
			--logDebug(math.abs(ttR))			
			if math.abs(ttR) < 40 then
				--logDebug("==")				
			else			
				--logDebug("~=")	
				return -1				
			end		
			--logDebug("\r\n ")			
		else  
			--logDebug(v)
			logDebug("多点比色参数不正确")	
			return -1
		end  				
    end  
	return 0
end 

function getOneAccountResource(account,password)
  
		--判断状态 如果是首页
		local ret = 1
		while ret > 0 do
			local currentState = getCurrentState()		
			if currentState == 1 then 
				homeLogic()	
			elseif currentState == 2 then 
				loginLogic(account, password)
			elseif currentState == 3 then 
				registerLogic()	
			elseif currentState == 4 then 
				gameInGameDailyJobLogic()--操作任务退出
				return 0				
			else 	
				unknowLogic()
								
			end					
				ret = getOneAccountResource(account,password)
				if ret > 0 then 
					mzLog("账号未完成获取资源")
				else 
				end				
		end	
				
		return 0 --操作成功		
end 

function getAccontsResource()
	accountNum = 0
	accountsArr = {}
	tmpI = 0	
	local path = ("var/touchelf/scripts/全民奇迹.txt")
	local file = io.open(path, "r")
		
	if file~=nil then 
		for line in file:lines() do
		--logDebug(line)
		accountsArr[tmpI]=line		
		tmpI=tmpI+1		
		end	
	file:close()
	end	
	logDebug("\n\n")
		
	mzLog("账号内容：")	
	for i, v in pairs(accountsArr) do  
		mzLog(v)
		mzLog(inputPassword)		
		local ret = 1
		
		if i >= tonumber(inputIndex-1) then 
		   	
			while ret > 0 do	 
			ret = getOneAccountResource(v,inputPassword)
			if ret > 0 then 
				mzLog("账号未完成获取资源")
			else 
			    mzLog("完成账号"..v.."获取资源")
			end				
		end	
		mzLog(v.."完成获取资源")
		
		reStartGame()
		mSleep(10000)
		end			
						
	end		
	logDebug("\n\n")	
   
end	

function test()
	--unknowLogic()	
end
function main()
	test()
	rotateScreen(0)	
	getAccontsResource()
	notifyMessage('完成所有账号的资源找回')
    mSleep(1730)	
end

